package com.ruoyi.web.controller.system;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.system.domain.TmcMaintenanceAttach;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ComplaintCertificate;
import com.ruoyi.system.service.IComplaintCertificateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 投诉凭证Controller
 * 
 * @author ruoyi
 * @date 2021-07-28
 */
@RestController
@RequestMapping("/system/certificate")
public class ComplaintCertificateController extends BaseController
{
    @Autowired
    private IComplaintCertificateService complaintCertificateService;

    /**
     * 查询投诉凭证列表
     */
//    @PreAuthorize("@ss.hasPermi('system:certificate:list')")
    @GetMapping("/list")
    public TableDataInfo list(ComplaintCertificate complaintCertificate)
    {
        startPage();
        List<ComplaintCertificate> list = complaintCertificateService.selectComplaintCertificateList(complaintCertificate);
        return getDataTable(list);
    }

    /**
     * 导出投诉凭证列表
     */
//    @PreAuthorize("@ss.hasPermi('system:certificate:export')")
    @Log(title = "投诉凭证", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ComplaintCertificate complaintCertificate)
    {
        List<ComplaintCertificate> list = complaintCertificateService.selectComplaintCertificateList(complaintCertificate);
        ExcelUtil<ComplaintCertificate> util = new ExcelUtil<ComplaintCertificate>(ComplaintCertificate.class);
        return util.exportExcel(list, "certificate");
    }

    /**
     * 获取投诉凭证详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:certificate:query')")
    @GetMapping(value = "/{ccId}")
    public AjaxResult getInfo(@PathVariable("ccId") Long ccId)
    {
        return AjaxResult.success(complaintCertificateService.selectComplaintCertificateById(ccId));
    }

    /**
     * 新增投诉凭证
     */
//    @PreAuthorize("@ss.hasPermi('system:certificate:add')")
    @Log(title = "投诉凭证", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add( MultipartFile file ,Long ccId){
        try {
            String profile = RuoYiConfig.getProfile();
            ComplaintCertificate complaint = new ComplaintCertificate();
            byte[] bytes = file.getBytes();
            //文件夹
            Path path2 = Paths.get(profile + "//ccId//" +ccId);
            //具体文件
            Path path = Paths.get(profile + "//ccId//" + ccId+"//" + file.getOriginalFilename());
            File file1 = path.toFile();
            //创建文件夹
            if (!file1.exists()) {
                path2.toFile().mkdirs();
            }
            if (file1.isFile()) {
                return AjaxResult.error("文件上传失败,文件存在");
            } else {
                Files.write(path, bytes);//创建文件
                if (true) {
                    complaint.setcId(ccId);
                    complaint.setCcUrl("profile/ccId/" + ccId+"/" + file.getOriginalFilename());
                    complaintCertificateService.insertComplaintCertificate(complaint);
                } else {
                    if (file1.isFile()) {
                        file1.delete();
                    }
                    return null;
                }
            }
        }catch (IOException e){

        }
        return success();
    }

    /**
     * 修改投诉凭证
     */
//    @PreAuthorize("@ss.hasPermi('system:certificate:edit')")
    @Log(title = "投诉凭证", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ComplaintCertificate complaintCertificate)
    {
        return toAjax(complaintCertificateService.updateComplaintCertificate(complaintCertificate));
    }

    /**
     * 删除投诉凭证
     */
//    @PreAuthorize("@ss.hasPermi('system:certificate:remove')")
    @Log(title = "投诉凭证", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ccIds}")
    public AjaxResult remove(@PathVariable Long[] ccIds)
    {
        return toAjax(complaintCertificateService.deleteComplaintCertificateByIds(ccIds));
    }
}
